package org.lyy.controller;

import org.lyy.entity.Order;
import org.lyy.entity.OrderDetail;
import org.lyy.entity.ProductInfo;
import org.lyy.service.OrderDetailService;
import org.lyy.service.OrderService;
import org.lyy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin//跨域
@RestController//@Controller+@ResponseBody
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ProductService productService;
    @RequestMapping("paid")
    public  Map<String, Object> orderTotal(HttpServletRequest request) {

        Map<String, Object> retMap = new HashMap<>();
        String or = request.getParameter("orderId");
        String orderA= request.getParameter("orderAmount");
        String orderS = request.getParameter("orderStatus");
        String orderT = request.getParameter("currentDate");
        Order o = new Order();
        o.setOrderId(or);
        o.setOrderAmount(orderA);
        o.setPayStatus(orderS);
        o.setCurrentDate(orderT);
        orderService.create(o);
        List<OrderDetail> od =  orderDetailService.selectOrderId(or);
        System.out.println(od);
        for(OrderDetail allAdd : od){
            Integer nu = Integer.valueOf(allAdd.getProductNum());
            List<ProductInfo> s = productService.findOne(allAdd.getProductId());
            for(ProductInfo pr: s){
                if(pr.getProductStock() < nu){
                    orderDetailService.delete(allAdd.getOrderId());
                    orderService.delete(allAdd.getOrderId());
                    retMap.put("msg", "error-val");
                    retMap.put("productName",pr.getProductName());
                    retMap.put("productStock",pr.getProductStock());
                    return retMap;
                }
            }
        }
        retMap.put("msg", "success-val");
        return retMap;
    }

    @RequestMapping("real")
    public Map<String, Object> changeStatus(String orderId,String orderAmount){
        Map<String, Object> retMap = new HashMap<>();
        Order newOrder = new Order();
        newOrder.setOrderId(orderId);
        newOrder.setOrderAmount(orderAmount);
        newOrder.setPayStatus("1");
        orderService.update(newOrder);

        List<OrderDetail> mingXi =  orderDetailService.selectOrderId(orderId);
        for(OrderDetail orderDetail : mingXi){
            Integer beijian = Integer.valueOf(orderDetail.getProductNum());
            List<ProductInfo> stock = productService.findOne(orderDetail.getProductId());
            for(ProductInfo productInfo : stock){
                Integer jianshu = Integer.valueOf(productInfo.getProductStock());
                Integer re = jianshu - beijian;
                ProductInfo newLine = new ProductInfo();
                if(re <= 0) {
                    newLine.setProductStatus(0);
                }
                else{
                    newLine.setProductStatus(1);
                }
                    newLine.setProductKind(productInfo.getProductKind());
                    newLine.setProductStock(re);
                    newLine.setProductName(productInfo.getProductName());
                    newLine.setProductId(productInfo.getProductId());
                    newLine.setProductPrice(productInfo.getProductPrice());
                    newLine.setProductIcon(productInfo.getProductIcon());
                    productService.update(newLine);
            }
        }
        retMap.put("msg","success-val");
        return retMap;
    }

    @RequestMapping("msg")
    public List<Order> getMsg(String payStatus){
        return orderService.searchStatus(payStatus);
    }

    @RequestMapping("msgDetail")
    public List<OrderDetail> getMsgDetail(String orderId){
        return orderDetailService.selectOrderId(orderId);
    }
}
